package com.yto.hrreport.event.buttons {
	
	import com.yto.hrreport.command.sysTemplatePanel;
	import com.yto.hrreport.event.buttons.superbutton.ButtonEvent;
	import com.yto.hrreport.pub.IProcessor;
	
	import mx.containers.Canvas;
	import mx.containers.HDividedBox;
	import mx.controls.Alert;
	import mx.controls.Tree;
	import mx.events.CloseEvent;
	import mx.managers.PopUpManager;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	public class TempDeleteButtonEvent extends ButtonEvent {
		
		private static var deleteEvent : TempDeleteButtonEvent = new TempDeleteButtonEvent();
		private static var parentName : String;
		private var headCanvas : Canvas;
		private var sysTree : Tree;
		
		public function TempDeleteButtonEvent() {
			super();
		}
		
		public static function getInstance(parentName : String) : TempDeleteButtonEvent {
			TempDeleteButtonEvent.parentName = parentName;
			return deleteEvent;
		}
		
		public function deleteEvent() : void {
			
			var parentCanvas : Canvas = Canvas(hrreport.ui.mainNavigator.getChildByName(parentName + "_canvas"));
			headCanvas = Canvas(parentCanvas.getChildByName(parentName + "_headCanvas"));
			var bodyCanvas : Canvas = Canvas(parentCanvas.getChildByName(parentName + "_bodyCanvas"));
			var hdivBox : HDividedBox = HDividedBox(bodyCanvas.getChildByName(parentName + "_hdivBox"));
			sysTree = Tree(hdivBox.getChildByName(parentName + "_sysTree"));	
			
			if(sysTree != null && sysTree.selectedItem != null) {
				Alert.show("确定删除此模板？" , "提示" , Alert.OK | Alert.CANCEL , hrreport.ui , check);
			}
			
		}
		
		private function check(event : CloseEvent) : void {
			
			if(event.detail == Alert.OK) {
				
				PopUpManager.addPopUp(hrreport.waitWindow , hrreport.ui , true);
				PopUpManager.centerPopUp(hrreport.waitWindow);
				
				var tableServer : RemoteObject = new RemoteObject();
				tableServer.destination = IProcessor.TableProcessor;
				tableServer.deleteBodyTableByPK(sysTree.selectedItem.nodeid);
				tableServer.addEventListener(ResultEvent.RESULT , deleteResult);
			}
			
		}
		
		private function deleteResult(event : ResultEvent) : void {
			
			var lastResult : String = event.result as String;
			
			if(lastResult != null && lastResult != "false") {
				
				var template : sysTemplatePanel = sysTemplatePanel.getInstance();
				template.reset();
				
				updateButtons(null , "tempDelete" , headCanvas , true);
				
			} else {
				Alert.show("未知异常，删除模板失败..." , "提示" , Alert.OK , hrreport.ui);
			}
			
			PopUpManager.removePopUp(hrreport.waitWindow);
			
		}
		
		
	}
}